package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import pojo.Student;
import service.StudentService;
import java.util.List;
import util.Page;

import javax.xml.transform.Result;

@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;
//    @RequestMapping("/list")
//   public String list( Model model){
//        //计算偏移量
//        List<Student> students = studentService.findAll();
//        //数据返回
//        model.addAttribute("students", students);
//        System.out.println(students);
//        return "student/list";
//    }
   /* @GetMapping
    public Page page(@RequestParam(defaultValue = "1") Integer currentPage,@RequestParam(defaultValue = "10") Integer pageSize){
        *//*Log.info("分页查询，参数：{}，{}",currentPage,pageSize);*//*
        return studentService.page(currentPage,pageSize);
    }*/
   @RequestMapping("/list/{currentPage}")
   public  String list(Student student,@PathVariable("currentPage")Integer currentPage,Model model){
        // 查询数据
        Page page = new Page();
        int totalCount = studentService.count(student);
        page.setTotalCount(totalCount);
        page.setCurrentPage(currentPage);
        page.setTotalPage();
        // 计算偏移量
        int offset = (currentPage - 1) * page.getPageSize();
        List<Student> students = studentService.findAll(student,offset,page.getPageSize());
        // 数据返回
        model.addAttribute("students", students);
        model.addAttribute("page", page);
        model.addAttribute("student", student);

        return "student/list";
    }
    @RequestMapping("/toadd")
    public String toAdd(){
        return "student/add";
    }
    @RequestMapping("/addSave")
    public String addSave(Student student){
        int result = studentService.addStudent(student);
        if (result > 0) {
            return "redirect:/student/list/1";
        } else {
            return "student/add";
        }
    }
    @RequestMapping("/delete/{sId}")
    public String deleteById(@PathVariable("sId") Integer sId){
        int result = studentService.deleteStudentById(sId);
        return result > 0 ? "redirect:/student/list/1" : "error";
    }
    @RequestMapping("/toupdate/{sId}")
    public String toUpdate(@PathVariable("sId") Integer sId, Model model){
        Student student = studentService.findById(sId);
        model.addAttribute("student", student);
        return "student/update";
    }
    @RequestMapping("/updateSave")
    public String updateSave(Student student){
        int result = studentService.updateStudent(student);
        if (result > 0) {
            return "redirect:/student/list/1";
        } else {
            return "student/update";
        }
    }
}
